import alchemy from "alchemy";
{{#if (eq webDeploy "alchemy")}}
{{#if (includes frontend "next")}}
import { Nextjs } from "alchemy/cloudflare";
{{else if (includes frontend "nuxt")}}
import { Nuxt } from "alchemy/cloudflare";
{{else if (includes frontend "svelte")}}
import { SvelteKit } from "alchemy/cloudflare";
{{else if (includes frontend "tanstack-start")}}
import { TanStackStart } from "alchemy/cloudflare";
{{else if (includes frontend "tanstack-router")}}
import { Vite } from "alchemy/cloudflare";
{{else if (includes frontend "react-router")}}
import { ReactRouter } from "alchemy/cloudflare";
{{else if (includes frontend "solid")}}
import { Vite } from "alchemy/cloudflare";
{{/if}}
{{/if}}
{{#if (eq serverDeploy "alchemy")}}
import { Worker } from "alchemy/cloudflare";
{{/if}}
{{#if (and (or (eq serverDeploy "alchemy") (and (eq webDeploy "alchemy") (eq backend "self"))) (eq dbSetup "d1"))}}
import { D1Database } from "alchemy/cloudflare";
{{/if}}
import { config } from "dotenv";

{{#if (and (eq webDeploy "alchemy") (eq serverDeploy "alchemy"))}}
config({ path: "./.env" });
config({ path: "./apps/web/.env" });
config({ path: "./apps/server/.env" });
{{else if (or (eq webDeploy "alchemy") (eq serverDeploy "alchemy"))}}
config({ path: "./.env" });
{{/if}}

const app = await alchemy("{{projectName}}");

{{#if (and (or (eq serverDeploy "alchemy") (and (eq webDeploy "alchemy") (eq backend "self"))) (eq dbSetup "d1"))}}
const db = await D1Database("database", {
	{{#if (eq orm "prisma")}}
	migrationsDir: "packages/db/prisma/migrations",
	{{else if (eq orm "drizzle")}}
	migrationsDir: "packages/db/src/migrations",
	{{/if}}
});
{{/if}}

{{#if (eq webDeploy "alchemy")}}
{{#if (includes frontend "next")}}
export const web = await Nextjs("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  bindings: {
    {{#if (eq backend "convex")}}
    NEXT_PUBLIC_CONVEX_URL: process.env.NEXT_PUBLIC_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    NEXT_PUBLIC_SERVER_URL: process.env.NEXT_PUBLIC_SERVER_URL || "",
    {{/if}}
    {{#if (eq dbSetup "d1")}}
    DB: db,
    {{else if (ne database "none")}}
    DATABASE_URL: alchemy.secret(process.env.DATABASE_URL),
    {{/if}}
    CORS_ORIGIN: process.env.CORS_ORIGIN || "",
    {{#if (eq auth "better-auth")}}
    BETTER_AUTH_SECRET: alchemy.secret(process.env.BETTER_AUTH_SECRET),
    BETTER_AUTH_URL: process.env.BETTER_AUTH_URL || "",
    {{/if}}
    {{#if (eq auth "clerk")}}
    CLERK_SECRET_KEY: alchemy.secret(process.env.CLERK_SECRET_KEY),
    {{/if}}
    {{#if (includes examples "ai")}}
    GOOGLE_GENERATIVE_AI_API_KEY: alchemy.secret(process.env.GOOGLE_GENERATIVE_AI_API_KEY),
    {{/if}}
    {{#if (eq payments "polar")}}
    POLAR_ACCESS_TOKEN: alchemy.secret(process.env.POLAR_ACCESS_TOKEN),
    POLAR_SUCCESS_URL: process.env.POLAR_SUCCESS_URL || "",
    {{/if}}
    {{#if (eq dbSetup "turso")}}
    DATABASE_AUTH_TOKEN: alchemy.secret(process.env.DATABASE_AUTH_TOKEN),
    {{/if}}
    {{#if (eq database "mysql")}}
    {{#if (eq orm "drizzle")}}
    DATABASE_HOST: process.env.DATABASE_HOST || "",
    DATABASE_USERNAME: process.env.DATABASE_USERNAME || "",
    DATABASE_PASSWORD: process.env.DATABASE_PASSWORD || "",
    {{/if}}
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "nuxt")}}
export const web = await Nuxt("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  bindings: {
    {{#if (eq backend "convex")}}
    NUXT_PUBLIC_CONVEX_URL: process.env.NUXT_PUBLIC_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    NUXT_PUBLIC_SERVER_URL: process.env.NUXT_PUBLIC_SERVER_URL || "",
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "svelte")}}
export const web = await SvelteKit("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  bindings: {
    {{#if (eq backend "convex")}}
    PUBLIC_CONVEX_URL: process.env.PUBLIC_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    PUBLIC_SERVER_URL: process.env.PUBLIC_SERVER_URL || "",
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "tanstack-start")}}
export const web = await TanStackStart("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  bindings: {
    {{#if (eq backend "convex")}}
    VITE_CONVEX_URL: process.env.VITE_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    VITE_SERVER_URL: process.env.VITE_SERVER_URL || "",
    {{/if}}
    {{#if (eq dbSetup "d1")}}
    DB: db,
    {{else if (ne database "none")}}
    DATABASE_URL: alchemy.secret(process.env.DATABASE_URL),
    {{/if}}
    CORS_ORIGIN: process.env.CORS_ORIGIN || "",
    {{#if (eq auth "better-auth")}}
    BETTER_AUTH_SECRET: alchemy.secret(process.env.BETTER_AUTH_SECRET),
    BETTER_AUTH_URL: process.env.BETTER_AUTH_URL || "",
    {{/if}}
    {{#if (eq auth "clerk")}}
    CLERK_SECRET_KEY: alchemy.secret(process.env.CLERK_SECRET_KEY),
    {{/if}}
    {{#if (includes examples "ai")}}
    GOOGLE_GENERATIVE_AI_API_KEY: alchemy.secret(process.env.GOOGLE_GENERATIVE_AI_API_KEY),
    {{/if}}
    {{#if (eq payments "polar")}}
    POLAR_ACCESS_TOKEN: alchemy.secret(process.env.POLAR_ACCESS_TOKEN),
    POLAR_SUCCESS_URL: process.env.POLAR_SUCCESS_URL || "",
    {{/if}}
    {{#if (eq dbSetup "turso")}}
    DATABASE_AUTH_TOKEN: alchemy.secret(process.env.DATABASE_AUTH_TOKEN),
    {{/if}}
    {{#if (eq database "mysql")}}
    {{#if (eq orm "drizzle")}}
    DATABASE_HOST: process.env.DATABASE_HOST || "",
    DATABASE_USERNAME: process.env.DATABASE_USERNAME || "",
    DATABASE_PASSWORD: process.env.DATABASE_PASSWORD || "",
    {{/if}}
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "tanstack-router")}}
export const web = await Vite("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  assets: "dist",
  bindings: {
    {{#if (eq backend "convex")}}
    VITE_CONVEX_URL: process.env.VITE_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    VITE_SERVER_URL: process.env.VITE_SERVER_URL || "",
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "react-router")}}
export const web = await ReactRouter("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  bindings: {
    {{#if (eq backend "convex")}}
    VITE_CONVEX_URL: process.env.VITE_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    VITE_SERVER_URL: process.env.VITE_SERVER_URL || "",
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{else if (includes frontend "solid")}}
export const web = await Vite("web", {
  {{#if (eq serverDeploy "alchemy")}}cwd: "apps/web",{{/if}}
  assets: "dist",
  bindings: {
    {{#if (eq backend "convex")}}
    VITE_CONVEX_URL: process.env.VITE_CONVEX_URL || "",
    {{else if (ne backend "self")}}
    VITE_SERVER_URL: process.env.VITE_SERVER_URL || "",
    {{/if}}
  },
  dev: {
    command: "{{packageManager}} run dev"
  }
});
{{/if}}
{{/if}}

{{#if (eq serverDeploy "alchemy")}}
export const server = await Worker("server", {
  {{#if (eq webDeploy "alchemy")}}cwd: "apps/server",{{/if}}
  entrypoint: "src/index.ts",
  compatibility: "node",
  bindings: {
    {{#if (eq dbSetup "d1")}}
    DB: db,
    {{else if (ne database "none")}}
    DATABASE_URL: alchemy.secret(process.env.DATABASE_URL),
    {{/if}}
    CORS_ORIGIN: process.env.CORS_ORIGIN || "",
    {{#if (eq auth "better-auth")}}
    BETTER_AUTH_SECRET: alchemy.secret(process.env.BETTER_AUTH_SECRET),
    BETTER_AUTH_URL: process.env.BETTER_AUTH_URL || "",
    {{/if}}
    {{#if (eq auth "clerk")}}
    CLERK_SECRET_KEY: alchemy.secret(process.env.CLERK_SECRET_KEY),
    {{/if}}
    {{#if (includes examples "ai")}}
    GOOGLE_GENERATIVE_AI_API_KEY: alchemy.secret(process.env.GOOGLE_GENERATIVE_AI_API_KEY),
    {{/if}}
    {{#if (eq payments "polar")}}
    POLAR_ACCESS_TOKEN: alchemy.secret(process.env.POLAR_ACCESS_TOKEN),
    POLAR_SUCCESS_URL: process.env.POLAR_SUCCESS_URL || "",
    {{/if}}
    {{#if (eq dbSetup "turso")}}
    DATABASE_AUTH_TOKEN: alchemy.secret(process.env.DATABASE_AUTH_TOKEN),
    {{/if}}
    {{#if (eq database "mysql")}}
    {{#if (eq orm "drizzle")}}
    DATABASE_HOST: process.env.DATABASE_HOST || "",
    DATABASE_USERNAME: process.env.DATABASE_USERNAME || "",
    DATABASE_PASSWORD: process.env.DATABASE_PASSWORD || "",
    {{/if}}
    {{/if}}
  },
  dev: {
		port: 3000,
	},
});
{{/if}}

{{#if (and (eq webDeploy "alchemy") (eq serverDeploy "alchemy"))}}
console.log(`Web    -> ${web.url}`);
console.log(`Server -> ${server.url}`);
{{else if (eq webDeploy "alchemy")}}
console.log(`Web    -> ${web.url}`);
{{else if (eq serverDeploy "alchemy")}}
console.log(`Server -> ${server.url}`);
{{/if}}

await app.finalize();
